Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python Functions → Function call stack

Python Functions

Function call stack

Python, like many other languages, uses a call stack to manage function calls. The call stack is a fundamental data structure – essentially a Last-In, First-Out (LIFO) stack – that keeps track of the active functions during program execution. When a function is called, its information (including local variables, parameters, and the return address) is pushed onto the stack. When the function completes, its information is popped off. This ensures proper order of execution and resource management.

1. Basic Function Call

Python call stack example def greet(name): print(f"Hello, {name}!") greet("Alice")

Output

Hello Alice!

Explanation 1. `greet("Alice")` is executed: The interpreter pushes a new frame onto the call stack. This frame contains: ⮚ The function's name (`greet`). ⮚ The argument passed (`"Alice"` which is assigned to the `name` parameter). ⮚ A return address (where execution should resume after `greet` finishes). 2. Inside `greet`: The `print` statement executes, displaying "Hello, Alice!". 3. `greet` completes: The frame for `greet` is popped off the stack. Execution resumes at the return address (which is the point after `greet("Alice")`).

2. Nested Function Calls

Nested Function Calls in Python def add(x, y): return x + y def multiply(a, b): result = add(a, b) * 2 #Nested call to 'add' return result print(multiply(3, 4))

Output

14

Explanation ⮚ `multiply(3,4)` is called: A frame for `multiply` is pushed, containing `a=3`, `b=4`, and the return address. ⮚ `add(a,b)` is called *within* `multiply`: A new frame for `add` is pushed *on top* of the `multiply` frame. This frame contains `x=3`, `y=4`, and its return address (inside `multiply`). ⮚ `add` executes: It calculates 7 and returns it. The `add` frame is popped. ⮚ Execution resumes in `multiply`: `result` becomes 14. ⮚ `multiply` completes: Its frame is popped. The final result (14) is printed.

3. Recursive Function Calls

Recursion is a powerful technique where a function calls itself. The call stack is crucial for managing recursive calls.
Recursive Function Calls in Python def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1) print(factorial(3))

Output

6
Explanation ⮚ `factorial(3)` is called: A frame is created. ⮚ `factorial(2)` is called (within `factorial(3)`): Another frame is pushed on top. ⮚ `factorial(1)` is called: Yet another frame. ⮚ `factorial(0)` is called: A frame is pushed. The base case is reached, it returns 1. ⮚ `factorial(0)`'s frame is popped. `factorial(1)` returns 1 * 1 = 1. Its frame is popped. ⮚ `factorial(2)` returns 2 * 1 = 2. Its frame is popped. ⮚ `factorial(3)` returns 3 * 2 = 6. Its frame is popped. The call stack manages the order of execution and ensures that each recursive call has its own set of local variables. Excessive recursion can lead to a `RecursionError` if the stack overflows (too many function calls without completion).

4. Exception Handling and the Stack

Exceptions are handled by unwinding the call stack.
Exception Handling and the Stack in Python def divide(x, y): try: result = x / y return result except ZeroDivisionError: return "Division by zero!" print(divide(10, 0))

Output

Division by zero!
If a `ZeroDivisionError` occurs within `divide`, the `try...except` block catches it. The interpreter unwinds the stack, popping frames until it finds a handler for the exception (in this case, within the `divide` function itself).

Visualizing the Stack

Imagine the call stack as a stack of plates. Each function call is a plate. Plates are added (pushed) as functions are called and removed (popped) as they finish. The top plate is always the currently executing function. If you try to add too many plates, the stack overflows (stack overflow error).

Tutorials